/* Copyright © 2010 LPRM
Universidade Federal do Espírito Santo - RNP-GT CWTools

This file is part of JoinUs!.

JoinUs! is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

JoinUs! is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with JoinUs!. If not, see <http://www.gnu.org/licenses/>. */
package android.isisufes.ProfileManager;

import java.io.StringReader;

import org.xml.sax.InputSource;

import android.app.Activity;
import android.isisufes.R;
import android.isisufes.ProfileManager.UIBuilders.ContactUI;
import android.isisufes.ProfileManager.UIBuilders.GeneralUI;
import android.isisufes.ProfileManager.UIBuilders.PersonalUI;
import android.isisufes.ProfileManager.UIBuilders.ProfessionalUI;
import android.isisufes.ProfileManager.UIBuilders.ProfileUI;
import android.isisufes.ProfileManager.UIBuilders.SocialUI;
import android.isisufes.ProfileManager.profile.Profile;
import android.isisufes.util.ServerCom;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.LayoutInflater;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.Spinner;

public class ProfileManager extends Activity {
	private LayoutInflater rootWindow;
	private ScrollView rootLayout;
	private Spinner dataTypeChooser;
	ProfileUI previous;
	int previousI = -1;
	ProfileUI[] typesViews;
	Profile[] profiles;
	boolean opened[];
	String userName, passWord;

	private void setupSpinner() {
		// loads the layout values
		typesViews = new ProfileUI[] {
				new GeneralUI(), new SocialUI(), new ContactUI(),
				new ProfessionalUI(), new PersonalUI()};

		profiles = new Profile[typesViews.length];

		// gets the spinner
		dataTypeChooser = (Spinner) this.findViewById(R.id.dataTypeChooser);
		ArrayAdapter<ProfileUI> options = new ArrayAdapter<ProfileUI>(this, android.R.layout.simple_spinner_item);

		// add objects to adapater
		for (ProfileUI s : typesViews)
			options.add(s);

		// binds adapater to spinner
		dataTypeChooser.setAdapter(options);

		View v = rootWindow.inflate(typesViews[0].getLayoutID(), rootLayout); //, null);
		typesViews[0].setUpWidgets(v);
		if (profiles[0] == null) {
			profiles[0] = typesViews[0].export();
			// xml = Request(profiles[i]);

			// ServerCom request = new ServerCom();
			String xml = ServerCom.GetAction(ProfileManager.this.userName,
					ProfileManager.this.passWord, ProfileManager.this.userName);

			if (xml != null) {
				InputSource is = new InputSource();
				is.setCharacterStream(new StringReader(xml));
				profiles[0].fromXML(is);
			}
		}
		
		typesViews[0].importPro(profiles[0]);
		
		// overloads the keylistener
		dataTypeChooser.setOnKeyListener(new OnKeyListener() {

			public boolean onKey(View view, int keycode, KeyEvent event) {
				String xml = null;
				if (keycode == KeyEvent.KEYCODE_DPAD_CENTER) {
					for (int i = 0; i < typesViews.length; i++) {
						// display the according subview
						if (typesViews[i].toString().compareTo(dataTypeChooser.getSelectedItem().toString()) == 0) {
							if (previous != null) {
								profiles[previousI] = previous.export();
							}
							previous = (ProfileUI) dataTypeChooser.getSelectedItem();
							if (i == previousI)
								return true;
							previousI = i;

							rootLayout.removeAllViews();
							View v = rootWindow.inflate(typesViews[i].getLayoutID(), rootLayout); //, null);
							typesViews[i].setUpWidgets(v);
							if (profiles[i] == null) {
								profiles[i] = typesViews[i].export();
								// xml = Request(profiles[i]);

								// ServerCom request = new ServerCom();
								xml = ServerCom.GetAction(ProfileManager.this.userName, ProfileManager.this.passWord, ProfileManager.this.userName);

								if (xml != null) {
									InputSource is = new InputSource();
									is.setCharacterStream(new StringReader(xml));
									profiles[i].fromXML(is);
								}
							}
							typesViews[i].importPro(profiles[i]);
							return true;
						}
					}
				}
				return false;
			}
		});
	}

	private void setUpButton() {	
		Button apply = (Button) this.findViewById(R.id.apply_action);
		apply.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				profiles[0]=typesViews[0].export();
				if (previous != null) {
					profiles[previousI] = previous.export();
				}
				for (int i = 0; i < typesViews.length; i++) {
					if (profiles[i] != null) {
						//Send(profiles[i].toXML(), profiles[i]);
						ServerCom.updateUser(ProfileManager.this.userName, ProfileManager.this.passWord,
								ProfileManager.this.userName, profiles[i].toXML());
					}
				}
			}
		});

		Button done = (Button) this.findViewById(R.id.done_action);
		done.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				profiles[0]=typesViews[0].export();
				if(previous!=null){
					profiles[previousI] = previous.export();
				}
				for (int i = 0; i < typesViews.length; i++) {
					if (profiles[i] != null) {
						//Send(profiles[i].toXML(), profiles[i]);
						ServerCom.updateUser(ProfileManager.this.userName, ProfileManager.this.passWord,
								ProfileManager.this.userName, profiles[i].toXML());
					}
				}
				finish();
			}
		});
	}

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle); // standard stuff
		setContentView(R.layout.main);

		Bundle b = this.getIntent().getExtras();
		this.userName = b.getString("un");
		this.passWord = b.getString("pw");

		rootWindow = LayoutInflater.from(this);  // get inflate windows manager
		rootLayout = (ScrollView) this.findViewById(R.id.dataTypeView); // get
		// relative
		// layout

		setupSpinner(); // setup the spinner for use
		setUpButton(); // set up the aplly button
	}
}
